package com.msb.mall.product.controller;

import com.msb.common.utils.PageUtils;
import com.msb.common.vo.CommonResult;
import com.msb.mall.core.entity.product.CategoryBrandRelationEntity;
import com.msb.mall.core.valid.groups.AddGroupsInterface;
import com.msb.mall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌分类关联
 *
 * @author leixu
 * @email xuleiwolf@163.com
 * @date 2024-04-19 20:08:58
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @GetMapping("/brands/list")
    public CommonResult categoryBrandRelation(
            @RequestParam(value = "catId", required = true, defaultValue = "0") Long catId) {
        return CommonResult.success(categoryBrandRelationService.categoryBrandRelation(catId));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public CommonResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return CommonResult.success(null, page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public CommonResult info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return CommonResult.success(null, categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public CommonResult save(@Validated(AddGroupsInterface.class) @RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
//        categoryBrandRelationService.save(categoryBrandRelation);

        return categoryBrandRelationService.saveSingle(categoryBrandRelation);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public CommonResult update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
//        categoryBrandRelationService.updateById(categoryBrandRelation);

        return categoryBrandRelationService.updateSingle(categoryBrandRelation);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public CommonResult delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return CommonResult.success();
    }

    /**
     * 品牌Id查询 关联类别信息
     *
     * @param brandId
     * @return
     */
    @GetMapping("/catelogList/{brandId}")
    public CommonResult categoryListByBrand(@PathVariable("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.findCategoryListByBrand(brandId);

        return CommonResult.success(list);
    }

}
